package com.xteam.youin_supplier.adpter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.model.TruckList;

import org.w3c.dom.Text;


/**
 * Created by Curry on 2015-12-16.
 */
public class TruckListViewAdapter extends RecyclerView.Adapter<TruckListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private TruckList truckList;
    public TruckListViewAdapter(Context context, TruckList truckList) {
        mContext = context;
        this.truckList = truckList;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public TruckListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.truck_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TruckListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);

        Log.i(GlobalData.TAG, "getIcon_url：" + truckList.getData().get(position).getIcon_url());
        Picasso.with(mContext.getApplicationContext()).load(truckList.getData().get(position).getIcon_url()).error(R.drawable.truck_icon)
                .into(holder.getTruckImg());
        holder.addOilTime.setText(truckList.getData().get(position).getTime());
        holder.licenceNum.setText(truckList.getData().get(position).getCar_num());
        holder.addMoneyNum.setText(truckList.getData().get(position).getQty()+"|"+truckList.getData().get(position).getPrice());

    }

    @Override
    public int getItemCount() {
        return truckList.getData().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView truckImg;
        private TextView licenceNum;
        private TextView addOilTime;
        private TextView addMoneyNum;
        public ViewHolder(View itemView) {
            super(itemView);
            truckImg = (ImageView) itemView.findViewById(R.id.truck_img);
            licenceNum = (TextView) itemView.findViewById(R.id.licence_num);
            addOilTime = (TextView) itemView.findViewById(R.id.add_oil_time);
            addMoneyNum = (TextView) itemView.findViewById(R.id.add_money_num);
        }

        public ImageView getTruckImg() {
            return truckImg;
        }

        public void setTruckImg(ImageView truckImg) {
            this.truckImg = truckImg;
        }

        public TextView getLicenceNum() {
            return licenceNum;
        }

        public void setLicenceNum(TextView licenceNum) {
            this.licenceNum = licenceNum;
        }

        public TextView getAddOilTime() {
            return addOilTime;
        }

        public void setAddOilTime(TextView addOilTime) {
            this.addOilTime = addOilTime;
        }

        public TextView getAddMoneyNum() {
            return addMoneyNum;
        }

        public void setAddMoneyNum(TextView addMoneyNum) {
            this.addMoneyNum = addMoneyNum;
        }
    }
}
